import {Component} from '@angular/core';
import {ArticleService} from "../providers/service/article.service";
import {Location} from "@angular/common";
import {ActivatedRoute, NavigationEnd, Router} from "@angular/router";
import {DomSanitizer} from "@angular/platform-browser";
import {MarkdownService} from "ngx-markdown";


@Component({
    selector: 'app-detail',
    templateUrl: './article.component.html'
})


export class ArticleComponent {

    article: any;//文章数据
    articleId: number;//文章id
    highlighted: boolean = false;

    constructor(
        private articleService: ArticleService,
        private activatedRoute: ActivatedRoute,
        private location: Location,
        private router: Router,
    ) {
        this.initData();
        this.article = {};
    }


    ngOnInit() {
        this.router.events
            .subscribe((event) => {
                if (event instanceof NavigationEnd) { // 当导航成功结束时执行
                    this.initData();
                }
            });
    }



    getArticleData() {
        this.articleService.getArticle({articleId: this.articleId})
            .subscribe(data => {
                if (data.status_code == 200) {
                    this.article = data.result;
                }
            })
    }

    initData() {
        this.articleId = this.activatedRoute.snapshot.params['id'] || 0;
        if (!this.articleId) {
            this.location.back();
        }
        this.getArticleData();
    }

}
